// ============================== 导出依赖 ============================== //

import UTIL from '../UTIL'
import MOCK from '../MOCK'

let nextId = 4

// ============================== 导出客户端 ============================== //

export class DemoUserPromiseClient {
  // —————————————————————————————— 构造方法 —————————————————————————————— //

  constructor(api) {
    this.api = api
    this.client_ = {}
  }

  // —————————————————————————————— 列表 —————————————————————————————— //

  query(DTO) {
    const { comQuery, content, leftId, date, beginDate, endDate, beginTime, endTime } = DTO.toObject()
    const { page, pageSize, orderBy } = comQuery
    let list = MOCK.USER

    if (content.name) list = list.filter((item) => item.name.includes(content.name))
    if (content.phone) list = list.filter((item) => item.phone.includes(content.phone))
    if (content.idNo) list = list.filter((item) => item.idNo.includes(content.idNo))
    if (content.roleId) list = list.filter((item) => item.roleId === content.roleId)
    if (content.organId) list = list.filter((item) => item.organId === content.organId)
    if (content.status) list = list.filter((item) => item.status === content.status)

    if (leftId) list = list.filter((item) => item.organIdsList.includes(leftId))
    if (date) list = list.filter((item) => item.beginDate === date)
    if (beginDate) list = list.filter((item) => item.beginDate >= beginDate)
    if (endDate) list = list.filter((item) => item.beginDate < endDate)
    if (beginTime) list = list.filter((item) => item.expireTime >= beginTime)
    if (endTime) list = list.filter((item) => item.expireTime < endTime)

    if (orderBy) {
      const [key, type] = orderBy
      list = [...list].sort((a, b) => (type === 'asc' ? a[key] - b[key] : b[key] - a[key]))
    }

    const contentList = list.slice(pageSize * (page - 1), pageSize * page)
    const resp = UTIL.listResp(list.length, JSON.parse(JSON.stringify(contentList)))
    return UTIL.promiseReturn(resp)
  }

  // —————————————————————————————— 单项 —————————————————————————————— //

  detail(DTO) {
    const { id } = DTO.toObject()
    const content = MOCK.USER.find((item) => item.id === id) || {}
    const resp = UTIL.oneResp(content)
    return UTIL.promiseReturn(resp)
  }

  // —————————————————————————————— 增 —————————————————————————————— //

  create(DTO) {
    const { content } = DTO.toObject()
    content.id = `${nextId++}`
    MOCK.USER.push(content)
    const resp = UTIL.submitResp()
    return UTIL.promiseReturn(resp)
  }

  // —————————————————————————————— 改 —————————————————————————————— //

  update(DTO) {
    const { content } = DTO.toObject()
    const matched = MOCK.USER.find((item) => item.id === content.id)
    if (matched) Object.assign(matched, content)
    const resp = UTIL.submitResp()
    return UTIL.promiseReturn(resp)
  }

  // —————————————————————————————— 删 —————————————————————————————— //

  delete(DTO) {
    const { id } = DTO.toObject()
    const index = MOCK.USER.findIndex((item) => item.id === id)
    if (index >= 0) MOCK.USER.splice(index, 1)
    const resp = UTIL.submitResp()
    return UTIL.promiseReturn(resp)
  }

  // —————————————————————————————— 状态 —————————————————————————————— //

  status(DTO) {
    const { id, status } = DTO.toObject()
    const matched = MOCK.USER.find((item) => item.id === id)
    if (matched) Object.assign(matched, { id, status })
    const resp = UTIL.submitResp()
    return UTIL.promiseReturn(resp)
  }

  // —————————————————————————————— 登录用户 —————————————————————————————— //

  tokenUser() {
    const id = (sessionStorage.TOKEN || '').slice(11)
    const content = MOCK.USER.find((item) => item.id === id)

    if (!content) throw new Error('token is wrong')

    const resp = UTIL.oneResp(content)
    return UTIL.promiseReturn(resp)
  }
}
